/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：IntegralLogDaoImpl.java
 * History:
 *       May 14, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.dao.Impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.xteam.ecsystem.dao.IIntegralLogDao;
import com.xteam.ecsystem.entities.BaseMemberBase;
import com.xteam.ecsystem.entities.LogIntegralLog;
import com.xteam.ecsystem.utils.JsonDateValueProcessor;
import com.xteam.ecsystem.utils.PageCenterStation;
import com.xteam.ecsystem.utils.StringUtil;

/**
 * 积分日志数据访问实现类
 * @author liuzhi
 *
 */
@Repository
@SuppressWarnings("unchecked")
public class IntegralLogDaoImpl extends DDLPrefix implements IIntegralLogDao {
	
	@Autowired
	private BaseDaoImpl baseDao;

	public void delIntegralLogsByIds(String ids) {
		Class clazz = null;
		
		try {
			clazz = Class.forName("com.xteam.ecsystem.entities.LogIntegralLog");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
		List list = new ArrayList();
		String oId[] = ids.split(",");
		for (String id : oId) {
			Object obj = this.baseDao.get(clazz, id);
			if (obj != null) {
				list.add(obj);
			}
		}
		this.baseDao.deleteAll(list);
	}

	public String getIntegralLogList(int pageIndex, int pageSize, String bmbNick, 
			String startTime, String endTime) {
		JSONObject jo = new JSONObject();
		String hql = "";
		String countHQL = "";
		
		try {
			List list = new ArrayList();
			
			hql = SQL_SELECT + "new " + LogIntegralLog.REF + " (lil.lilId, lil.lilMember, lil.lilIncrement, " 
				+ "lil.lilTime, lil.lilRemark, bmb.bmbNick)" + SQL_FROM + LogIntegralLog.REF + " as lil,"
				+ BaseMemberBase.REF + " as bmb" + SQL_WHERE + "lil.lilMember = bmb.bmbId ";
			
			countHQL = SQL_SELECT + " count(*) " + SQL_FROM + LogIntegralLog.REF + " as lil, " 
					 + BaseMemberBase.REF + " as bmb" + SQL_WHERE + "lil.lilMember = bmb.bmbId";
			
			if (StringUtil.isNotNullOrEmpty(bmbNick)) {
				hql += SQL_AND + " bmb.bmbNick " + SQL_LIKE + " ? ";
				countHQL += SQL_AND + " bmb.bmbNick " + SQL_LIKE + " ? ";
				list.add("%" + bmbNick + "%");

			}
						
			if (StringUtil.isNotNullOrEmpty(startTime)) {
				hql += " and lil.lilTime >=? "; 
				countHQL += " and lil.lilTime >=? ";
			    list.add(StringUtil.strToDate(startTime, null));
			 }
			
			if (StringUtil.isNotNullOrEmpty(endTime)) {
				hql += " and lil.lilTime <=? "; 
				countHQL += " and lil.lilTime <=? ";
			    list.add(StringUtil.strToDate(endTime, null));
			 }
			
			hql += SQL_ORDERBY + "lil.lilTime desc";

			PageCenterStation<?> p = (PageCenterStation<?>) this.baseDao
					.queryList(countHQL, hql, list.toArray(), 
							pageIndex * pageSize, pageSize);
	
			// 率先格式化日期，在取数据时便不必再作处理。
			JsonConfig jsonConfig = new JsonConfig();
			jsonConfig.registerJsonValueProcessor(Date.class,
					new JsonDateValueProcessor("yyyy-MM-dd HH:mm:ss"));
			
			jo = JSONObject.fromObject(p, jsonConfig);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return jo.toString();
	}

	public void saveIntegralLogs(String lilMember, int lilIncrement, String lilRemark) {
		LogIntegralLog integralLog = new LogIntegralLog();
		integralLog.setLilMember(lilMember);
		integralLog.setLilIncrement(lilIncrement);
		integralLog.setLilRemark(lilRemark);
		integralLog.setLilTime(new Date());
		
		this.baseDao.save(integralLog);
	}
	
	public void delIntegralLogByCon(String condition){
		String hql = "";
		
		try {
			if (StringUtil.isNotNullOrEmpty(condition)) {
				int type = Integer.parseInt(condition);
				
				switch (type) {
				case 0: // 一周之前
					hql = SQL_DELETE + SQL_FROM + LogIntegralLog.REF + SQL_WHERE 
							+ "DATEDIFF(NOW(), lilTime) > 7";  
					break;
				case 1: // 一个月之前
					hql = SQL_DELETE + SQL_FROM + LogIntegralLog.REF + SQL_WHERE 
					+ "DATEDIFF(NOW(), lilTime) > 30"; 
					break;
				case 2: // 三个月之前
					hql = SQL_DELETE + SQL_FROM + LogIntegralLog.REF + SQL_WHERE 
					+ "DATEDIFF(NOW(), lilTime) > 90"; 
					break;
				case 3: // 半年之前
					hql = SQL_DELETE + SQL_FROM + LogIntegralLog.REF + SQL_WHERE 
					+ "DATEDIFF(NOW(), lilTime) > 180"; 
					break;
				case 4: // 一年之前
					hql = SQL_DELETE + SQL_FROM + LogIntegralLog.REF + SQL_WHERE 
					+ "DATEDIFF(NOW(), lilTime) > 360"; 
					break;
				default:
					break;
				}
				
				this.baseDao.executeUpdate(hql);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
